<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <script>
    // 阶段 1：JavaScript 核心能力
    // 手写 函数柯里化（currying）
    function add(a) {
      // return a + b + c;
      return function (b) {
        return function (c) {
          return a + b + c;
        };
      };
    }
    const res = add(1)(2)(3);
    console.log(res);

    // 实现：参数传递5个即可实现累加
    // sum(1)(2)(3)(4)(5)
    // sum(1)(2,3)(4)(5)
    // sum(1,2,3)(4,5)
    // let nums = [];
    function sum(...args) {
      nums.push(...args);
      if (nums.length >= 5) {
        const res = nums.slice(0, 5).reduce((a, b) => a + b, 0);
        nums = []; // 计算完毕重置数组
        return res;
      } else {
        return sum;
      }
    }

    // 基于上述代码实现sumMarker自定义累加个数
    function sumMaker(length) {
      let nums = [];
      function sum(...args) {
        nums.push(...args);
        if (nums.length >= length) {
          // console.log(nums);
          const res = nums.slice(0, length).reduce((a, b) => a + b, 0);
          nums = []; // 计算完毕重置数组
          return res;
        } else {
          return sum;
        }
      }
      return sum;
    }

    const sum6 = sumMaker(6);
    const res6 = sum6(1, 2, 3)(4, 5, 6);
    // const sum4 = sumMaker(4);
    // sum4(1, 2, 3, 4);
    console.log(res6, 'res6');

    const typeOfTest = type => {
      return thing => typeof thing === type;
    };
    // 函数预制参数
    const isString = typeOfTest('string');
    const isNumber = typeOfTest('number');
    const isBoolean = typeOfTest('boolean');

    console.log(isString('123'));
    console.log(isNumber(123));
    console.log(isBoolean(true));
  </script>
  <body></body>
</html>
